/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.base;

import java.io.File;

public class ClassPath {
	private final Code[] allCode;

	public ClassPath() {
		this(System.getProperty("java.class.path"), new CodeFactory(), new Tokenizer());
	}

	ClassPath(String classPath, CodeFactory codeFactory, Tokenizer tokenizer) {
		this.allCode = codeFactory.create(tokenizer.uniqueTokens(classPath, File.pathSeparator));
	}

	public byte[] findClass(String className) {
		return visit(new ClassFindingCodeVisitor(className)).byteCode();
	}

	<V extends CodeVisitor> V visit(V codeVisitor) {
		for (Code code : allCode) {
			if (codeVisitor.visit(code).isTerminal()) {
				break;
			}
		}

		return codeVisitor;
	}
}
